package com.animee.week03.slice;
import com.animee.week03.ResourceTable;
import com.animee.week03.bean.User;
import com.animee.week03.db.DBManager;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.data.rdb.RdbStore;
import ohos.global.i18n.utils.TextRecognitionUtils;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class SaveUserAbilitySlice extends AbilitySlice {
    TextField nameTf,telTf,emailTf,infoTf;
    Button saveBtn,clearBtn;
    RadioContainer sexRc;
    RadioButton mRb,fRb;
    Image headImg;
    HiLogLabel label = new HiLogLabel(HiLog.INFO, 0x1122, "animee");
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_saveuser);
        initView();
    }

    private void initView() {
        nameTf = findComponentById(ResourceTable.Id_save_tf_uname);
        telTf = findComponentById(ResourceTable.Id_save_tf_tel);
        emailTf = findComponentById(ResourceTable.Id_save_tf_email);
        infoTf = findComponentById(ResourceTable.Id_save_tf_info);
        saveBtn = findComponentById(ResourceTable.Id_save_btn_bc);
        clearBtn = findComponentById(ResourceTable.Id_save_btn_clear);
        sexRc = findComponentById(ResourceTable.Id_save_rc_sex);
        mRb = findComponentById(ResourceTable.Id_save_rb_m);
        fRb = findComponentById(ResourceTable.Id_save_rb_f);
        headImg = findComponentById(ResourceTable.Id_save_iv);

        MyClickListener listener = new MyClickListener();
        saveBtn.setClickedListener(listener);
        clearBtn.setClickedListener(listener);
    }

    class MyClickListener implements Component.ClickedListener{
        @Override
        public void onClick(Component component) {
            int id = component.getId();
            switch (id) {
                case ResourceTable.Id_save_btn_bc:
                    String name = nameTf.getText();
                    String tel = telTf.getText();
                    String email = emailTf.getText();
                    String info = infoTf.getText();
                    String sex = "女";
                    int markedButtonId = sexRc.getMarkedButtonId();
                    if (markedButtonId==ResourceTable.Id_save_rb_m){
                        sex = "男";
                    }
                    boolean empty = emptyByInput(name,tel,email);
                    if (empty){  //有空数据
                        showToast("输入数据不能为空~");
                    }else{
                        User user = new User(name, tel, email, sex, info);
                        //保存对象
                        saveUserToDB(user);
                        showToast("保存成功！");
                        clearAll();
                    }
                    break;
                case ResourceTable.Id_save_btn_clear:
                    clearAll();
                    break;
            }

        }
    }

    private void clearAll() {
        nameTf.setText("");//将文本内容设置未空
        telTf.setText("");
        emailTf.setText("");
        infoTf.setText("");
        mRb.setChecked(true);
    }

    private void showToast(String msg) {
        new ToastDialog(getContext())
                .setText(msg)
                .setAlignment(LayoutAlignment.CENTER)
                .show();
    }

    // 将输入的内容保存到数据库当中
    private void saveUserToDB(User user) {
        RdbStore rdbStore = DBManager.getRdbStore(this);
        String sql = "insert into usertb (name,gender,phone,email,info) values (?,?,?,?,?)";
        Object[]data = {user.getName(),user.getGender(),user.getPhone(),user.getEmail(),user.getInfo()};
        rdbStore.executeSql(sql,data);
    }

    private boolean emptyByInput(String name, String tel, String email) {
        if (name==null||name.equals("")){
            return true;
        }
        if (tel==null||tel.equals("")){
            return true;
        }
        if (email==null||email.equals("")){
            return true;
        }
        return  false;
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
